#include <iostream>
#include <QtGui>
#include "../item.h"
#include "../itemfactory.h"
#include "../mapitem.h"
#include "../mapitemfactory.h"
#include "createmapitem.h"
#include "itemexist.h"
#include <string>

CreateMapItem::CreateMapItem(QWidget *parent) : QDialog(parent)
{
	// Initialize all variables
	id = new QLabel(tr("Id:"));
	name = new QLabel(tr("Name:"));
	desc = new QLabel(tr("Description:"));
	pic = new QLabel(tr("Picname:"));
	walkable = new QLabel(tr("Is Walkable:"));
	portal = new QLabel(tr("Is Portal:"));

	idEdit = new QLineEdit;
	nameEdit = new QLineEdit;
	descEdit = new QLineEdit;
	picEdit = new QLineEdit;
	walkEdit = new QCheckBox(tr(""));
	portalEdit = new QCheckBox(tr(""));

	id->setBuddy(idEdit);
	name->setBuddy(nameEdit);
	desc->setBuddy(descEdit);
	pic->setBuddy(picEdit);
	walkable->setBuddy(walkEdit);

	saveButton = new QPushButton(tr("&Save"));
	
	// Connect all the events to approprite slots
	connect(saveButton, SIGNAL(clicked()), this, SLOT(saveCurrent()));
	connect(idEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setId(const QString &)));
	connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setName(const QString &)));
	connect(picEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setPic(const QString &)));
	connect(descEdit, SIGNAL(textChanged(const QString &)), this, SLOT(setDesc(const QString &)));

	// Layout of the window
	QVBoxLayout *textLayout = new QVBoxLayout;
	textLayout->addWidget(id);
	textLayout->addWidget(name);
	textLayout->addWidget(desc);
	textLayout->addWidget(pic);
	textLayout->addWidget(walkable);
	textLayout->addWidget(portal);
	QVBoxLayout *inputLayout = new QVBoxLayout;
	inputLayout->addWidget(idEdit);
	inputLayout->addWidget(nameEdit);
	inputLayout->addWidget(descEdit);
	inputLayout->addWidget(picEdit);
	inputLayout->addWidget(walkEdit);
	inputLayout->addWidget(portalEdit);
	QHBoxLayout *textInput = new QHBoxLayout;
	textInput->addLayout(textLayout);
	textInput->addLayout(inputLayout);
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(textInput);
	mainLayout->addWidget(saveButton);
	setLayout(mainLayout);

	setWindowTitle(tr("Create/Edit MapItem"));
	setFixedHeight(sizeHint().height());
}

// Save the changed/new mapitem blueprint to disk
void CreateMapItem::saveCurrent(){
	
	try{
		// Create new blueprint mapitem, make changes and save to disk
		MapItemFactory *mapItemFactory = new MapItemFactory();
		mapItemFactory->create(inputId);
		mapItemFactory->setName(inputName);
		mapItemFactory->setDescription(inputDesc);
		mapItemFactory->setPicName(inputPic);
		
		// There is never any item on a mapitem by default
		mapItemFactory->setItem(false);
		
		// Set walkable depending on the checkbox
		if(walkEdit->isChecked())
		{
			mapItemFactory->setWalkable(true);
		}
		else
		{
			mapItemFactory->setWalkable(false);
		}

		// Set portal depending on  checkbox
		if(portalEdit->isChecked())
		{
			mapItemFactory->setPortal(true);
		}
		else
		{
			mapItemFactory->setPortal(false);
		}

		mapItemFactory->save();
		//delete mapItemFactory;	
	}
	catch(mapitemfactory_error &texterror)
	{

		if(overwrite())
		{
			try
			{
				// Load the mapitem blueprint from disk, make changes and save
				MapItemFactory *mapItemFactory = new MapItemFactory();
				mapItemFactory->load(inputId);
				mapItemFactory->setName(inputName);
				mapItemFactory->setDescription(inputDesc);
				mapItemFactory->setPicName(inputPic);
				
				// Set walkable depending on the checkbox
				if(walkEdit->isChecked())
				{
					std::cout << "true";
					mapItemFactory->setWalkable(true);
				}
				else
				{
					std::cout << "false";
					mapItemFactory->setWalkable(false);
				}
				
				mapItemFactory->save();
				//delete mapItemFactory;
			}
			catch(mapitemfactory_error &texterror)
			{
				loadError();
			}
			
		}
		
	}
}

// Save user input to Id
void CreateMapItem::setId(const QString & changedId)
{
	inputId = changedId.toStdString();
}

// Save user input to Name
void CreateMapItem::setName(const QString & changedId)
{
	inputName = changedId.toStdString();
}

// Save user input to Picture
void CreateMapItem::setPic(const QString & changedId)
{
	inputPic = changedId.toStdString();
}

// Save user input to Description
void CreateMapItem::setDesc(const QString & changedId)
{
	inputDesc = changedId.toStdString();
}

// Overwrite warning message
bool CreateMapItem::overwrite()
{
	int r = QMessageBox::warning(this, tr("MapItem"),
				     tr("This mapitem already exists.\n"
					"Do you want to overwrite it?"),
				     QMessageBox::Yes | QMessageBox::Default,
				     QMessageBox::No);
	if (r == QMessageBox::Yes) {
		return true;
	} 
	else if (r == QMessageBox::No) {
		return false;
	}
}

// Load warning message
void CreateMapItem::loadError()
{
	QMessageBox::warning(this, tr("MapItem"),
			     tr("Error while saving changes.\n"),
			     QMessageBox::Ok | QMessageBox::Default);
}
